package com.xxzb.pack0823.线程同步.互斥同步;

public class 死锁 {
    private static final Object LOCK1=new Object();
    private static final Object LOCK2=new Object();

    public void fn1(){
        System.out.println(Thread.currentThread().getName()+":等待获得锁1");
        synchronized (LOCK1){
            System.out.println(Thread.currentThread().getName()+":获得锁1，等待获得锁2");
            try {
                //制造死锁
                Thread.sleep(5);//线程休眠
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (LOCK2){
                System.out.println(Thread.currentThread().getName()+":获得锁2");
            }
            System.out.println(Thread.currentThread().getName()+":释放锁2");
        }
        System.out.println(Thread.currentThread().getName()+":释放锁1");
    }

    public void fn2(){
        System.out.println(Thread.currentThread().getName()+":等待获得锁2");
        synchronized (LOCK2){
            System.out.println(Thread.currentThread().getName()+":获得锁2，等待获得锁1");
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (LOCK1){
                System.out.println(Thread.currentThread().getName()+":获得锁1");
            }
            System.out.println(Thread.currentThread().getName()+":释放锁1");
        }
        System.out.println(Thread.currentThread().getName()+":释放锁2");
    }
}
